<?php
/**
 * 支付日志类
 *
 * @auther 大耳牛
 * 2017-2-16
 **/
namespace Xijie\Model;
use Think\Model;
class PaylogModel extends Model{
	//定义表名称
	protected $tableName = 'paylog';
	
	/**
	 * 获取会员列表总数
	 *
	 **/
	public function getPaylogCount($where){
		return M('paylog')->where($where)->count();
	}
	
	/**
	 * 获取会员列表
	 *
	 **/
	public function getPaylogList($page, $where){
		return M('paylog')->where($where)
					->order('id desc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}
	
	public function getPayLogInfoByOrderSn($ordersn){
		$count = M('paylog')->where(array('order_sn' => $ordersn))->count();
		//writeLog('count: '. $this->_sql());
		return $count;
	}
	
	/**
	 * 添加支付日志
	 **/
	public function addPayLogInfo($data){
		//writeLog('Insert Data: '. $data);
		$row = M('paylog')->data($data)->add();
		//writeLog('Insert log: '. $this->_sql());
		return $row;
	}
	
	public function addOrderInfo($data){
		return M('order')->data($data)->add();
	}
	
	public function editOrderInfoByOrderSn($orderSn, $state, $payType=1){
		$row = M('order')->where(array(
			'order_sn' => $orderSn
		))->setField(array(
			'status' => $state,
			'pay_id' => $payType
		));
		
		return $row;
	}
	
	/**
	 * 获取详情
	 **/
	public function getOrderInfoByOrderSn($ordersn){
		return M('order')->where(array('order_sn' => $ordersn))->find();
	}
	
	public function getOrderCountBySn($ordersn){
		return M('order')->where(array(
			'order_sn' => $ordersn
		))->count();
	}
	
	public function getOrderCountByWhere($where){
		return M('order')->where($where)->count();
	}
	
	//开始查询数据
	public function getPayLogGroupByType($where=array()){
		return $this->where($where)
					->field('sum(point) as points, type_id')
					->group('type_id')
					->select();
	}
	
	/**
	 * 获取开始数与结束数
	 **/
	public function getEqmentLogByWhere($where=array(), $order=array()){
		return M('eqment_log')->where($where)->order($order)->limit(1)->getField('last_number');
	}
}
